package com.golden.procedure.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.SingleRequest;
import com.golden.procedure.domain.SiteTemplateDO;
import com.golden.procedure.domain.WorkSchedulingSettingDO;
import com.golden.procedure.domain.WorkSiteSeatDO;
import com.golden.procedure.query.SchedulingQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.ISiteTemplateService;
import com.golden.procedure.service.IWorkSeatSchedulingService;
import com.golden.procedure.service.IWorkSiteSchedulingService;
import com.golden.procedure.service.IWorkSiteSeatService;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 摄控设置 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.controller <br>
 *       <b>ClassName:</b> SchSettingController <br>
 *       <b>Date:</b> 2018年10月22日 下午6:11:55
 */
@RestController
@RequestMapping(value = "/api/schSetting")
public class SchSettingController extends BaseController {

	@Autowired
	IWorkSiteSchedulingService workSiteSchedulingService;
	@Autowired
	IWorkSiteSeatService workSiteSeatService;
	@Autowired
	IWorkSeatSchedulingService workSeatSchedulingService;
	@Autowired
	ISiteTemplateService siteTemplateService;

	public static String encodeObject(SingleResponse<WorkSchedulingSettingDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodeSiteSeatsPage(PageResponse<WorkSiteSeatDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "创建摄控设置", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间(年月日时间戳)", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间(年月日时间戳)", required = true, dataType = "String"),
			@ApiImplicitParam(name = "dayoff", value = "休息日（周一-周日，多个使用，逗号分隔）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isUseTemplate", value = "是否使用模板（0-不使用； 1-使用）", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "templateID", value = "模板ID（使用模板时）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "maxSeatGroup", value = "最大工位分组（新创建）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "planStartOnStr", value = "拍摄时段(8:00,10:00)逗号分隔（新创建）", required = false, dataType = "String") })
	@RequestMapping(value = { "/add.json" }, method = RequestMethod.POST)
	public String add(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO schedulingSetting = request.getData();

		// {"data":
		// {"siteID":1,"dayoff":"6,7","isUseTemplate":0,"maxSeatGroup":5,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000","planStartOnStr":"09:30,14:00,16:00"},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		schedulingSetting.setAuthorID(9);
		schedulingSetting.setAuthor("李玉玲");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSiteSchedulingService.add(schedulingSetting);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData(schedulingSetting);
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "保存设置", notes = "保存设置")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间(年月日时间戳)", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间(年月日时间戳)", required = true, dataType = "String") })
	@RequestMapping(value = "/save.json", method = RequestMethod.POST)
	public String saveSetting(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO settingDO = request.getData();

		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000"},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSiteSchedulingService.saveSetting(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询摄控设置", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控开始日期(年月日时间戳)", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束日期(年月日时间戳)", required = true, dataType = "String") })
	@RequestMapping(value = { "/query.json" }, method = RequestMethod.POST)
	public String query(@RequestBody QueryRequest<SchedulingQueryDO> request) {

		SchedulingQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}

		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000"},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		PageResponse<WorkSiteSeatDO> response = new PageResponse<WorkSiteSeatDO>();
		ResultDO resultDO = workSiteSchedulingService.query(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkSiteSeatDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((Integer) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeSiteSeatsPage(response, PLANINTEXT);
	}

	@ApiOperation(value = "摄控设置保存为模板", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "templateName", value = "摄控模板名称（保存为新模板）", required = false, dataType = "String"),
			@ApiImplicitParam(name = "destTemplateID", value = "模板ID（待更新的模板ID）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控开始日期(年月日时间戳)", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束日期(年月日时间戳)", required = true, dataType = "String") })
	@RequestMapping(value = { "/saveToTemplate.json" }, method = RequestMethod.POST)
	public String saveToTemplate(@RequestBody SingleRequest<SiteTemplateDO> request) {

		SiteTemplateDO domain = request.getData();

		// {"data":
		// {"siteID":1,"templateName":"模板0928","schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000"},"version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		domain.setAuthorID(9);
		domain.setAuthor("李玉玲");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = siteTemplateService.addFromSetting(domain);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "新增seat时间", notes = "新增seat时间")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间（年月日时间戳）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间（年月日时间戳）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "siteSeatID", value = "工位分组id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "planStartOnStr", value = "时段", required = true, dataType = "String") })
	@RequestMapping(value = "/addSeat.json", method = RequestMethod.POST)
	public String addSeat(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO settingDO = request.getData();

		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000","siteSeatID":2382,"planStartOnStr":"11:00"},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		settingDO.setAuthorID(9);
		settingDO.setAuthor("李玉玲");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();

		ResultDO resultDO = workSeatSchedulingService.addSeat(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "编辑seat时间", notes = "模板编辑seat时间")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间（年月日时间戳）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间（年月日时间戳）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "siteSeatID", value = "工位分组id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "oldPlanStartOn", value = "原有时段", required = true, dataType = "String"),
			@ApiImplicitParam(name = "planStartOnStr", value = "新时段", required = true, dataType = "String") })
	@RequestMapping(value = "/editSeat.json", method = RequestMethod.POST)
	public String editSeat(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO settingDO = request.getData();

		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000","siteSeatID":2382,"oldPlanStartOn":"09:30","planStartOnStr":"09:00"},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		settingDO.setModifiID(9);
		settingDO.setModifi("李玉玲");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSeatSchedulingService.editSeat(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "启用/禁用seat时间", notes = "启用/禁用seat时间")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "siteSeatID", value = "工位分组id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "planStartOnStr", value = "时段", required = true, dataType = "String"),
			@ApiImplicitParam(name = "status", value = "状态：1启用/-1禁用", required = true, dataType = "Integer") })
	@RequestMapping(value = "/seatStatus.json", method = RequestMethod.POST)
	public String seatStatus(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO settingDO = request.getData();
		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000","siteSeatID":2382,"planStartOnStr":"11:00","status":0},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		settingDO.setModifiID(9);
		settingDO.setModifi("test");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSeatSchedulingService.seatStatus(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "删除seat", notes = "删除seat时间")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "planStartOnStr", value = "时段", required = true, dataType = "String") })
	@RequestMapping(value = "/deleteSeat.json", method = RequestMethod.POST)
	public String deleteSeat(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO settingDO = request.getData();
		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000","siteSeatID":2382,"planStartOnStr":"11:00","status":0},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		settingDO.setModifiID(9);
		settingDO.setModifi("test");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSeatSchedulingService.deleteSeat(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "删除工位分组", notes = "删除工位分组（无工单的分组）")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "siteSeatIDs", value = "工位分组ids", required = true, dataType = "Integer[]") })
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public String deleteGroup(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO settingDO = request.getData();

		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000","siteSeatIDs":[2381,2382]},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		settingDO.setModifiID(9);
		settingDO.setModifi("李玉玲");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSiteSeatService.deleteGroup(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "清除设置", notes = "删除所有分组")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStart", value = "摄控起始时间", required = true, dataType = "String"),
			@ApiImplicitParam(name = "schedulingDateEnd", value = "摄控结束时间", required = true, dataType = "String") })
	@RequestMapping(value = "/deleteSetting.json", method = RequestMethod.POST)
	public String deleteSetting(@RequestBody SingleRequest<WorkSchedulingSettingDO> request) {

		WorkSchedulingSettingDO settingDO = request.getData();

		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000"},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		settingDO.setModifiID(9);
		settingDO.setModifi("李玉玲");

		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSiteSchedulingService.deleteSetting(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

}
